package levels
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	import utilities.Wave;
	
	import enemies.*;
	import interactions.*;
	import items.*;
	import treasures.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Tutorial_1 extends Level
	{
		private static const GODOWN:uint = 0;
		private static const GORIGHT:uint = 1;
		private static const GOUP:uint = 2;
		private static const GOLEFT:uint = 3;
		private static const GODOWNAGAIN:uint = 4;
		private static const STARTED:uint = 5;
		private static const TELEPORTED:uint = 6;
		private static const SPAWNED:uint = 7;
		
		private var _state:uint;
		private var _timer:FlxDelay;
		private var _keys:FlxSprite;
		private var _thisIsYou:FlxText;
		private var _pauseInstruction:FlxText;
		private var _moved:Boolean;
		private var _teleporter:Entity;
		
		override public function create():void
		{
			super.create();
			
			_checkGameOver = false;
			
			_state = GODOWN;
			_moved = false;
			
			_timer = new FlxDelay(0);
			_keys = new FlxSprite();
			_keys.loadGraphic(Assets.ImgKeys, true, false, 463, 250);
			_keys.x = 275;
			_keys.addAnimation("NONE", [0], 6);
			_keys.addAnimation("UP", [0, 3], 6);
			_keys.addAnimation("DOWN", [0, 1], 6);
			_keys.addAnimation("LEFT", [0, 2], 6);
			_keys.addAnimation("RIGHT", [0, 4], 6);
			_misc.add(_keys);
			
			_maze.load(MazeData.tutorial_1);
			Player.spawnAt(1, 5);
			Tracker.lives = 0;
			Tracker.minScore = 5;
			Tracker.mapScore = 1000;
			
			Animation.finger(Player.midpoint, 1);
			
			// In the rooms
			addEntity(new LootBag(4, 14));
			addEntity(new LootBag(4, 16));
			addEntity(new LootBag(5, 15));
			addEntity(new LootBag(6, 14));
			addEntity(new LootBag(6, 16));
			
			addEntity(new LootBag(17, 11));
			addEntity(new LootBag(21, 19));
			
			var t1:Teleporter = new Teleporter(5, 5);
			var t2:Entity = new Entity(17, 15);
			t2.loadGraphic(Assets.ImgGrave, false);
			t2.toggleLight();
			t1.other = t2;
			addEntity(t1);
			_misc.add(t2);
			_teleporter = t2;
			
			_thisIsYou = new FlxText(0, 50, FlxG.width, "This is you!");
			_thisIsYou.font = "cooper_black";
			_thisIsYou.size = 15;
			_thisIsYou.exists = true;
			add(_thisIsYou);
			
			_pauseInstruction = new FlxText(75, 170, 200, "Press ENTER to\npause the game");
			_pauseInstruction.font = "cooper_black";
			_pauseInstruction.size = 15;
			_pauseInstruction.exists = true;
			add(_pauseInstruction);
			
			var keyLights:Light = new Light(15, 6);
			keyLights.scale.make(10, 10);
			
			var keyLights2:Light = new Light(25, 6);
			keyLights2.scale.make(10, 10);
			
			gameStart();
		}
		
		override public function update():void
		{
			super.update();
			
			if (_player.isMoving)
			{
				_moved = true;
			}
			
			if (_moved)
			{
				_thisIsYou.exists = false;
				
			}
			else
			{
				Animation.finger(_player.getMidpoint(), 1);
			}
			
			if (_state == GODOWN)
			{
				_keys.play("DOWN");
				if (_player.velocity.y > 0)
				{
					_state = GORIGHT;
				}
			}
			
			else if (_state == GORIGHT)
			{
				_keys.play("RIGHT");
				if (_player.velocity.y < 0)
				{
					_state = GODOWN;
				}
				if (_player.velocity.x > 0)
				{
					_state = GOUP;
				}
			}
			else if (_state == GOUP)
			{
				_keys.play("UP");
				if (_player.velocity.y < 0)
				{
					_state = GOLEFT;
				}
				if (_player.velocity.x < 0)
				{
					_state = GORIGHT;
				}
			}
			else if (_state == GOLEFT)
			{
				_keys.play("LEFT");
				if (_player.velocity.y > 0)
				{
					_state = GOUP;
				}
				if (_player.velocity.x < 0)
				{
					_state = GODOWNAGAIN;
				}
			}
			else if (_state == GODOWNAGAIN)
			{
				_keys.play("DOWN");
				if (_player.velocity.x > 0)
				{
					_state = GOLEFT;
				}
				if (_player.velocity.y > 0)
				{
					_state = STARTED;
				}
				if (_player.x > FlxG.width / 2)
				{
					_state = TELEPORTED;
					_timer.reset(1000);
					var corner1:FlxPoint = Maze.getTileCenterByIndex(17, 15);
					Animation.greenBox(corner1, new FlxPoint(corner1.x + Maze.tileWidth, corner1.y + Maze.tileHeight));
				}
			}
			else if (_state == STARTED)
			{
				_keys.play("NONE");
				if (_player.velocity.y < 0)
				{
					_state = GODOWNAGAIN;
				}
				
				if (_player.x > FlxG.width / 2)
				{
					_state = TELEPORTED;
					_timer.reset(1000);
					var corner2:FlxPoint = Maze.getTileCenterByIndex(17, 15);
					Animation.greenBox(corner2, new FlxPoint(corner2.x + Maze.tileWidth, corner2.y + Maze.tileHeight));
				}
			}
			else if (_state == TELEPORTED)
			{
				_teleporter.alpha = 0.5;
				_keys.play("NONE");
				_keys.alpha = 0.25;
				if (_timer.hasExpired)
				{
					Animation.intruderWarning();
					_checkGameOver = true;
					_state = SPAWNED;
					addEntity(new Robber(28, 19));
					//Animation.finger(_maze.getTileMidPoint(28, 19));
					var protect:FlxText = new FlxText(500, 375, FlxG.width, "    Protect\nthe treasure!");
					protect.size = 20;
					add(protect);
				}
			}
			else if (_state == SPAWNED)
			{
				
			}
		}
	}
}